<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-form size="small" label-width="70px" v-formkey="{query:toQuery}">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="名称">
                <el-input v-model="query.name" placeholder="输入品牌名称进行搜索"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      <rrOperation :crud="crud" />
    </div>
      <crudOperation :permission="permission">
      </crudOperation>
    </div>
    <div class="body-container">
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="600px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="编码" prop="code">
            <el-input v-model="form.code" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="商品品牌类型">
            <el-select v-model="form.type" class="edit-input" placeholder="请选择" style="width: 370px;">
              <el-option
                  v-for="(item,index) in dict.t_bas_goods_brand_type"
                  :key="index"
                  v-if="item.seq>0"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="所属部门">
            <treeselect
                v-model="form.deptId"
                :options="depts"
                placeholder="选择部门"
                style="width: 370px"
                :normalizer="normalizer"
            >
            </treeselect>
          </el-form-item>
          <el-form-item label="关联供应商">
            <zSelect :value.sync="form.customerId" url="api/dropDown/basCustomerList" :params="{isContact:'1,2'}" style="width:  370px;"/>
          </el-form-item>

          <el-form-item label="关联税种">
            <el-select
                v-model="form.taxId"
                style="width: 370px;"
                placeholder="选择税种">
              <el-option
                  v-for="(item,index) in taxList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>

          <!--          <el-form-item label="税种id">-->
          <!--            <el-input v-model="form.taxId" style="width: 370px;"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="税种编码">-->
          <!--            <el-input v-model="form.taxCode" style="width: 370px;"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="税种名称">-->
          <!--            <el-input v-model="form.taxName" style="width: 370px;"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="税率">-->
          <!--            <el-input v-model="form.taxRate" style="width: 370px;"/>-->
          <!--          </el-form-item>-->
          <el-form-item label="排序">
            <el-input-number v-model="form.seq" :min="1" label="排序" style="width: 370px;"></el-input-number>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" class="edit-input" placeholder="请选择" style="width: 370px;">
              <el-option
                  v-for="(item,index) in dict.common_status"
                  :key="index"
                  v-if="item.seq>0"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" type="textarea" style="width: 370px;"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>

      <vue-context-menu :contextMenuData="contextMenuData"
                        @tableConfig="tableConfig"
                        @deletedata="deletedata"></vue-context-menu>
      <!-- 表格配置 -->
      <el-dialog title="表格配置"
                 :visible.sync="tableConfigShow"
                 width="60%">
        <j-table-column-config :columns.sync="crud.header"  @tabSave="tabSave" @pit="pit" />
      </el-dialog>

      <!--表格渲染-->
      <el-table ref="table"  border
                height="700"
                show-summary
                @cell-dblclick="row=>crud.toEdit(row)"
                v-loading="crud.loading"
                :data="crud.data"
                size="small"
                style="width: 100%;"
                v-if="isShowTable"
                @header-dragend="changeColWidth"
                @contextmenu.native="showMenu"
                :summary-method="crud.sumRow"
                @row-click="crud.selectRow"
                :row-style="crud.rowClass"
                :row-class-name="crud.tableRowClassName"
                @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
        <el-table-column :render-header="crud.renderHeader" type="index" width="60" label="序号"/>
        <el-table-column
          v-if="item.visible"
          v-for="(item, index) in crud.header"
          :key="`col_${index}`"
          :prop="item.prop"
          :label="columnLabel(item)"
          :width="item.width + 'px'"
          :data-porp='item.prop'
          @contextmenu.native="showMenu"
          @header-dragend="changeColWidth"
        >
          <!-- 自定义渲染模版 -->
          <template slot-scope="scope">
            <!-- 当列key为 sort 时，渲染一个手柄icon -->
            <template v-if="item.prop =='sort'">
              <div class="handle_move">
                <i :class="scope.row['sort']" />
              </div>
            </template>
            <template v-else>
              <span v-if="item.type == 1" style="margin-left: 0px">{{scope.row[`${item.prop}`]}}</span>
              <span v-if="item.type == 2" style="margin-left: 0px">{{ parseTime(scope.row[`${item.prop}`])}}</span>
              <span v-if="item.type == 3" style="margin-left: 0px">{{ getDict(`this.dict.label.${item.dictionary}["${scope.row[item.prop]}"]`) }}</span>
            </template>
          </template>
        </el-table-column>
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('code')" prop="code" label="编码"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('name')" prop="name" label="名称"/>-->
<!--        &lt;!&ndash;        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('type')" prop="type" label="商品品牌类型"/>&ndash;&gt;-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('deptName')" prop="deptName" label="部门名称"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('customerName')" prop="customerName" label="供应商名称"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('taxName')" prop="taxName" label="税种名称"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('remark')" prop="remark" label="备注"/>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('status')" prop="status" label="状态">-->
<!--          <template slot-scope="scope">-->
<!--            {{ dict.label.common_status[scope.row.status] }}-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column :render-header="crud.renderHeader" v-permission="['admin','basGoodsBrand:edit','basGoodsBrand:del']" label="操作" width="150px"-->
<!--                         align="center">-->
<!--          <template slot-scope="scope">-->
<!--            <udOperation-->
<!--                :data="scope.row"-->
<!--                :permission="permission"-->
<!--            />-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudBasGoodsBrand from '@/api/erp/basGoodsBrand'
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getDepts} from "@/api/system/dept";
import {getTaxList} from "@/api/erp/basTax";
import {searchCustomer} from "@/api/erp/basCustomer";
import Treeselect from '@riophae/vue-treeselect'

import { publicListKey } from "@/utils/key";
import {getHeaderProperties} from "@/api/erp/headerProperties";
import {tablemixins} from "@/mixins/tablemixins";
import  jTableColumnConfig  from "@/components/jTableColumnConfig/jTableColumnConfig";

// crud交由presenter持有
const defaultCrud = CRUD({title: '商品品牌', url: 'api/basGoodsBrand', sort: 'code,asc', crudMethod: {...crudBasGoodsBrand}})
const defaultForm = {
  id: null,
  code: null,
  name: null,
  type: null,
  deptId: null,
  deptCode: null,
  deptName: null,
  customerId: null,
  customerCode: null,
  customerName: null,
  taxId: null,
  taxCode: null,
  taxName: null,
  taxRate: null,
  seq: null,
  status: null,
  remark: null,
  createUserId: null,
  createUserCode: null,
  createUserName: null,
  createTime: null,
  updateUserId: null,
  updateUserCode: null,
  updateUserName: null,
  updateTime: null
}
export default {
  name: 'BasGoodsBrand',
  components: {pagination, crudOperation, rrOperation, udOperation, Treeselect,jTableColumnConfig},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud(),publicListKey,tablemixins],
  dicts: ['t_bas_goods_brand_type', 'common_status'],
  data() {
    return {

      permission: {
        add: ['admin', 'basGoodsBrand:add'],
        edit: ['admin', 'basGoodsBrand:edit'],
        del: ['admin', 'basGoodsBrand:del'],
        import: ['admin', 'basGoodsBrand:import'],
        export: ['admin', 'basGoodsBrand:export'],
        copy: ['admin', 'basGoodsBrand:copy'],
        upload: ['admin', 'basGoodsBrand:upload'],
        download: ['admin', 'basGoodsBrand:download'],
        enable: ['admin', 'basGoodsBrand:enable'],
        disable: ['admin', 'basGoodsBrand:disable'],
      },
      rules: {
        name: [
          {required: true, message: '名称不能为空', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '编码不能为空', trigger: 'blur'}
        ],
      },
      loading: false,
      userList: [],
      taxList: [], depts: []
    }
  },
  watch: {},
  updated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })},
  created() {
    this.getDepts();
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      getTaxList().then(res => {
        this.taxList = res.data.content
      })
      this.getDepts()
    },
    // 获取弹窗内部门数据
    getDepts() {
      getDepts({status: 1}).then(res => {
        this.depts = res
      })
    },
    normalizer(node) {
      //去掉children=[]的children属性
      if (node.children == null || node.children.length == 0) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    //远程搜索用户
    remoteMethod(query) {
      if (query !== '') {
        this.loading = true;
        searchCustomer({keyword: query}).then(res => {
          this.loading = false
          this.userList = res.data.content;
          this.$forceUpdate()
        })
      } else {
        this.userList = [];
      }
    }

  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
